package FlashServer;

import java.util.LinkedList;

public class ThreadPool 
{
	final private LinkedList<SocketHandler> FWorkerslist = new LinkedList<SocketHandler>();
	final private LinkedList<SocketHandler> FUsersList = new LinkedList<SocketHandler>();
	final private LinkedList<SocketHandler> FRoomsList = new LinkedList<SocketHandler>();

	public ThreadPool(int APoollSize) 
	{
		for (int i = 0; i < APoollSize; i++) 
		{
			SocketHandler vWorker = new SocketHandler(this, FUsersList);			
			vWorker.setName("SocketThread_" + i);
			vWorker.getMessageProcessor().Init(FRoomsList);
			vWorker.start();
			FWorkerslist.add(vWorker);
		}
	}

	public SocketHandler getWorkerThread() 
	{
		SocketHandler vWorker = null;
		synchronized (FWorkerslist) 
		{
			if (!FWorkerslist.isEmpty())
				vWorker = FWorkerslist.remove();
		}
		return vWorker;
	}

	public void ReturnToPool(SocketHandler AWorker) 
	{
		synchronized (FWorkerslist) 
		{		
			FWorkerslist.add(AWorker);
		}
	}
}